package cn.alise.qdp.service.admin;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.alise.qdp.mapper.admin.RoleMenuMapper;
import cn.alise.qdp.model.role.RoleMenu;

@Service
@Transactional(rollbackFor = Exception.class)
public class RoleMenuService {

	@Resource(name = "roleMenuMapper")
	private RoleMenuMapper roleMenuMapper;
	
	public List<RoleMenu> gets(Map<String, Object> args){
		return this.roleMenuMapper.gets(args);
	}
	
	public List<Long> list(RoleMenu args) {
		return this.roleMenuMapper.list(args);
	}
	
	public List<RoleMenu> page(Map<String, Object> args) {
		return this.roleMenuMapper.page(args);
	}
	
	public int total(RoleMenu args) {
		return this.roleMenuMapper.total(args);
	}
	
	public int delete(RoleMenu args) {
		return this.roleMenuMapper.delete(args);
	}
	
	public int save(RoleMenu args) {
		return this.roleMenuMapper.insert(args);
	}
	
	public int update(RoleMenu args) {
		return this.roleMenuMapper.update(args);
	}
	
	public void grant(Map<String, Object> args) {
		
		RoleMenu roleMenu = new RoleMenu();
		roleMenu.setRoleId(Long.parseLong(args.get("roleId").toString()));
		
		// 先删除所有已授权角色菜单
		roleMenuMapper.delete(roleMenu);
		
		// 新增授权角色菜单
		// 拆分menuIds
		String[] menuArr = args.get("menuIds").toString().split(",");
		if(menuArr[0] != "") {
			for(int i=0; i<menuArr.length; i++) {
				
				roleMenu.setRoleId(Long.parseLong(args.get("roleId").toString()));
				roleMenu.setMenuId(Long.parseLong(menuArr[i]));
				roleMenu.setCreatedTime(new Timestamp(new Date().getTime()));
				roleMenu.setCreater(Long.parseLong(args.get("creater").toString()));
				
				roleMenuMapper.insert(roleMenu);
			}
		}
	}
}
